//
//  MJDIYHeader.m
//  MJRefreshExample
//
//  Created by MJ Lee on 15/6/13.
//  Copyright © 2015年 小码哥. All rights reserved.
//

#import "MJDIYHeader.h"

@interface MJDIYHeader()
@property (weak, nonatomic) UILabel *label;
@property (nonatomic, strong) UIImageView  *imageView;
@property (weak, nonatomic) UIActivityIndicatorView *loading;

@end

@implementation MJDIYHeader
#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50+22;
    
    // 添加label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = CXBColor(51, 51, 51);
    label.font = [UIFont boldSystemFontOfSize:14];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    self.label.text=@"今日油价";
    
    // 添加label
    UILabel *label1 = [[UILabel alloc] init];
    label1.textColor = CXBColor(51, 51, 51);;
    label1.font = [UIFont boldSystemFontOfSize:14];
    label1.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label1];
    self.label1 = label1;
     self.label1.text=@"今日油价";
    // 添加label
    UILabel *label2 = [[UILabel alloc] init];
    label2.textColor = CXBColor(51, 51, 51);;
    label2.font = [UIFont boldSystemFontOfSize:14];
    label2.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label2];
    self.label2 = label2;
     self.label2.text=@"今日油价";
    // 添加label
    UILabel *label3 = [[UILabel alloc] init];
    label3.textColor = CXBColor(51, 51, 51);;
    label3.font = [UIFont boldSystemFontOfSize:14];
    label3.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label3];
    self.label3 = label3;
     self.label3.text=@"今日油价";
    
    // 添加label
    UILabel *label4 = [[UILabel alloc] init];
    label4.textColor =CXBColor(51, 51, 51);;
    label4.font = [UIFont boldSystemFontOfSize:14];
    label4.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label4];
    self.label4 = label4;
    self.label4.text=@"今日油价";
    
    UIImageView *imageView=[[UIImageView alloc] init];
    imageView.image=[UIImage imageNamed:@"down"];
    [self addSubview:imageView];
    self.imageView=imageView;
    
    // loading
    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    loading.hidden=YES;
    [self addSubview:loading];
    self.loading = loading;
    
}
#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    CGFloat with=(WHIDTH-50)/4;
    self.label.frame = CGRectMake(WHIDTH/2-with/2, 0+22, with, 25);
    self.label1.frame = CGRectMake(10, 25+22, with, 25);
    self.label2.frame = CGRectMake(20+with, 25+22, with, 25);
    self.label4.frame = CGRectMake(30+2*with, 25+22, with, 25);
    self.label3.frame = CGRectMake(40+3*with, 25+22, with, 25);
    self.imageView.frame=CGRectMake(WHIDTH/2-11, 0, 22, 22);
     self.loading.frame=CGRectMake(WHIDTH/2-11, 0, 22, 22);
    
    if (WHIDTH==320) {
        with=(WHIDTH-25)/4;
        self.label.frame = CGRectMake(WHIDTH/2-with/2, 0+22, with, 25);
        self.label1.frame = CGRectMake(5, 25+22, with, 25);
        self.label2.frame = CGRectMake(10+with, 25+22, with, 25);
        self.label4.frame = CGRectMake(15+2*with, 25+22, with, 25);
        self.label3.frame = CGRectMake(20+3*with, 25+22, with, 25);
        self.label1.font = CXBFont(14);
        self.label2.font = CXBFont(14);
        self.label3.font = CXBFont(14);
        self.label4.font = CXBFont(14);
    }
    

}
#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];

}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];

}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;

    switch (state) {
        case MJRefreshStateIdle:
        {
            self.imageView.hidden=NO;
            self.loading.hidden=YES;
                self.imageView.image=[UIImage imageNamed:@"down"];
        }
            
        
//            self.label.text = @"赶紧下拉吖(开关是打酱油滴)";
            break;
        case MJRefreshStatePulling:
        {
            self.imageView.hidden=NO;
             self.loading.hidden=YES;
            self.imageView.image=[UIImage imageNamed:@"up"];
           
        
        }
   

            break;
        case MJRefreshStateRefreshing:
        {
            
            self.imageView.hidden=YES;
                 self.loading.hidden=NO;
                 [self.loading startAnimating];
        
        }
   
            break;
        default:
            break;
    }
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
//    // 1.0 0.5 0.0
//    // 0.5 0.0 0.5
//    CGFloat red = 1.0 - pullingPercent * 0.5;
//    CGFloat green = 0.5 - 0.5 * pullingPercent;
//    CGFloat blue = 0.5 * pullingPercent;
//    self.label.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

@end
